﻿--[[
   @brief    sub account mgr,to get the sub account list ,update the sub account list
   @author   seanyxie
   @version  1.0
   @date     2015-03-31
--]]

-- 在原来的C++逻辑，是要通过fib_ui_multi_account，调用multi_account_mgr,最后调用到login_mgr，并且有个结果返回，现在用game_library来代替fib_ui_mult_account

sub_account_mgr={
    to_del_uin = 0;
    get_sub_account_list = function()
        local loginlib =
            base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
        subacclist = ierd_tgp.login_mgr.SubAccountListPod:new_local();
        loginlib:GetSubAccountList(subacclist);
        ierd_tgp.tpf_ui.Log("[main_view]subacclist "..subacclist.count);
        for i=1,subacclist.count do 
            ierd_tgp.tpf_ui.Log("[main_view]subacclist uin:"..subacclist.uins[i-1]);
          --  loginlib:DeleteSubAccountRecord(subacclist.uins[i-1]);
        end;
        return subacclist;
    end;

    del_sub_account = function(uin)
        sub_account_mgr.to_del_uin = uin;
        local param = {
                first_line = LoadStr(ID_DEL_SUB_ACCOUNT_TIP),
                icon = "ONEIMAGE:bottom_bar\\tips_icon\\blue_icon.png;Stretch;0;",
                has_cancel_btn = 1,
                tip_title = LoadStr(ID_DEL_SUB_ACCOUNT_TITLE),
                ok_broadcast = "del_sub_account_ok"
            }
            event_center.send_event(sub_account_login_event.SUB_ACCOUNT_SHOW_TIPS, 
                base_utility.table_to_json(param));
            -- 添加事件监听
            comm_center.listen_broadcast("del_sub_account_ok", sub_account_mgr.ok_btn_clicked);
    end;

    ok_btn_clicked = function()
        local loginlib =
      base_utility.get_component_adapter("ILogin_mgr",
          "ierd_tgp.login_mgr.GetLoginMgr();");
        loginlib:DeleteSubAccountRecord(sub_account_mgr.to_del_uin);
        ierd_tgp.tpf_ui.Require('super_multi_account_mgr\\control.lua');
        local param_data= {};
        param_data.uin = sub_account_mgr.to_del_uin;
        param_data.sub_accounts_list = super_multi_account_mgr_control.get_sub_accounts_list();
        comm_center.send_broadcast("i_broadcast_super_multi_account_remove_sub_account_node", param_data);
    end;

    login_sub_account = function(gameid, uin, op_from)
        -- 深度优化提示框并不会改变游戏状态，但是却会影响游戏启动流程

        if (launchpage_control and launchpage_control.launchpage_running) or
            (preloadpage_control and preloadpage_control.preloadpage_running) then
            ui_utility.show_tips({desc=LoadStr("XIAOHAO_SUBACCOUNT_TIPS")});
            return;
        end

        --[[切换小号登录游戏流程
            调用game_library_adapter的try_switch_account接口  
            |______失败:抛出事件到lua,提示后流程终止
            |______成功：通过C++ game_library内部调用login_mgr登录小号的接口，然后在game_library内部处理登陆成功/失败的消息
                        |______失败：抛出事件到lua,提示给用户【主要是sso登录的一些失败信息，密码过期等信息】
                            |______成功：继续走 game_library的choose_zone接口，进入选区，于此同时，login_mgr可能要处理 验证密保等通知消息
        --]]
        local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        gamelib:try_switch_account(gameid, uin, op_from);

        -- record current running uin for mult account need know sub account state
        -- 因改造接口，暂时不支持这用使用，待CF需要的时候，再修改其他的使用方式
        --[[local plugin_mgr =
            base_utility.get_component_adapter("IPlugin_mgr",
                "ierd_tgp.tgp_plugin.GetPluginMgrModule();");
        if nil ~= plugin_mgr then
            plugin_mgr:set_asura_current_uin(uin);
        end
        --]]
    end;

}